import {createRouter, createWebHistory} from 'vue-router'
import routes from './route'
import request from "@/utils/request.js";
const dataPage = ()=> import("../views/data/index.vue")
async function getDict() {
    let data = await request.get('/dict_arr')
    routes.forEach(route => {
        if(route.path === '/data') {
            // 动态添加数据维护
            for (let d of data) {
                route.children.push({
                    path: `${d.dict_key}`,
                    key: `/data/${d.dict_key}`,
                    name: d.dict_key,
                    label: d.dict_value,
                    component: dataPage,
                    meta: {
                        params:d,
                        title: d.dict_value,
                        keepAlive: true,
                    },
                })
            }

        }
    })
}

await getDict()
const router = createRouter({
    history: createWebHistory(),
    routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
    // 如果访问的不是登录页，且用户未登录，则重定向到登录页
    if (to.path !== '/login' && !localStorage.getItem('user')) {
        next('/login')
    } else {
        next()
    }
})

export default router
